<?php

session_start();
require 'connection.php';
require('fpdf/PDF.php');
require('phpexcel/Excel.php');

if ($_POST["report"] == "report_purcharse_pdf") {
    report_purcharse_pdf();
} elseif ($_POST["report"] == "report_purcharse_excel") {
    report_purcharse_excel();
} elseif ($_POST["report"] == "purcharse_supplier_pdf") {
    purcharse_supplier_pdf();
} elseif ($_POST["report"] == "purcharse_supplier_excel") {
    purcharse_supplier_excel();
} 
elseif ($_POST["report"] == "report_sale_pdf") {
    report_sale_pdf();
} elseif ($_POST["report"] == "report_sale_excel") {
    report_sale_excel();
} elseif ($_POST["report"] == "report_sale_client_pdf") {
    report_sale_client_pdf();
} elseif ($_POST["report"] == "report_sale_client_excel") {
    report_sale_client_excel();
} 

function report_purcharse_pdf() {
    $pdf = new PDF();
    $pdf->title = "Reporte de Compras";
    $pdf->AddPage();
    //suma de col 192
    $pdf->setHeaderCol(20, 5, 'Número');
    $pdf->setHeaderCol(20, 5, 'Fecha');
    $pdf->setHeaderCol(20, 5, 'RUC');
    $pdf->setHeaderCol(72, 5, 'Proveedor');
    $pdf->setHeaderCol(20, 5, 'SubTotal');
    $pdf->setHeaderCol(20, 5, 'IGV');
    $pdf->setHeaderCol(20, 5, 'Total');
    $pdf->Ln();

    $sql = "SELECT h.serie, h.number, h.date, o.code, o.name, h.total, h.subtotal, h.igv tax, h.operator_id  
                FROM import_document h
                INNER JOIN operators o ON o.id = h.operator_id
                WHERE h.type = 1 AND o.type = 1 AND month(date) = :month";
    try {
        $stmt = getConnection()->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
        $stmt->bindParam("month", $_POST["month"]);
        $stmt->execute();
        while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
            $pdf->setDetailCol(20, 5, str_pad($row[0], 3, "0", STR_PAD_LEFT) . " - " . $row[1]);
            $pdf->setDetailCol(20, 5, $row[2]);
            $pdf->setDetailCol(20, 5, $row[3]);
            $pdf->setDetailCol(72, 5, $row[4]);
            $pdf->setDetailCol(20, 5, $row[5]);
            $pdf->setDetailCol(20, 5, $row[6]);
            $pdf->setDetailCol(20, 5, $row[7]);
            $pdf->Ln();
        }
        $stmt = null;
    } catch (PDOException $e) {
        print $e->getMessage();
    }
    $pdf->Output("Reporte de Compras.pdf", "D");
}
function report_purcharse_excel() {
    $excel = new Excel();
    $excel->title('Reporte de Compras'); //31 max

    $excel->getActiveSheet()->setTitle();
    $excel->setHeaderCol('B2', 'Número')->setColWidth('B', 20);
    $excel->setHeaderCol('C2', 'Fecha')->setColWidth('C', 20);
    $excel->setHeaderCol('D2', 'RUC')->setColWidth('D', 20);
    $excel->setHeaderCol('E2', 'Proveedor')->setColWidth('E', 50);
    $excel->setHeaderCol('F2', 'SubTotal')->setColWidth('F', 20);
    $excel->setHeaderCol('G2', 'IGV')->setColWidth('G', 20);
    $excel->setHeaderCol('H2', 'Total')->setColWidth('H', 20);

    $count = 3;
    $sql = "SELECT h.serie, h.number, h.date, o.code, o.name, h.total, h.subtotal, h.igv tax, h.operator_id  F
                ROM import_document h
                INNER JOIN operators o ON o.id = h.operator_id
                WHERE h.type = 1 AND o.type = 1 AND month(date) = :month";
    try {
        $stmt = getConnection()->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
        $stmt->bindParam("month", $_POST["month"]);
        $stmt->execute();
        while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
            $excel->setDetailCol('B' . $count, str_pad($row[0], 3, "0", STR_PAD_LEFT) . " - " . $row[1]);
            $excel->setDetailCol('C' . $count, $row[2]);
            $excel->setDetailCol('D' . $count, $row[3]);
            $excel->setDetailCol('E' . $count, $row[4]);
            $excel->setDetailCol('F' . $count, $row[5]);
            $excel->setDetailCol('G' . $count, $row[6]);
            $excel->setDetailCol('H' . $count, $row[7]);
            $count++;
        }
        $stmt = null;
    } catch (PDOException $e) {
        print $e->getMessage();
    }

    $excel->output();
}
function purcharse_supplier_pdf() {
    $pdf = new PDF();
    $pdf->title = "Reporte de Compras";
    $pdf->AddPage();
    //suma de col 192
    $pdf->setHeaderCol(20, 5, 'Número');
    $pdf->setHeaderCol(20, 5, 'Fecha');
    $pdf->setHeaderCol(20, 5, 'RUC');
    $pdf->setHeaderCol(72, 5, 'Proveedor');
    $pdf->setHeaderCol(20, 5, 'SubTotal');
    $pdf->setHeaderCol(20, 5, 'IGV');
    $pdf->setHeaderCol(20, 5, 'Total');
    $pdf->Ln();

    $sql = "SELECT h.serie, h.number, h.date, o.code, o.name, h.total, h.subtotal, h.igv tax, h.operator_id  
                FROM import_document h
                INNER JOIN operators o ON o.id = h.operator_id
                WHERE h.type = 1 AND o.type = 1 AND h.operator_id = :operator_id";
    try {
        $stmt = getConnection()->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
        $stmt->bindParam("operator_id", $_POST["operator_id"]);
        $stmt->execute();
        while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
            $pdf->setDetailCol(20, 5, str_pad($row[0], 3, "0", STR_PAD_LEFT) . " - " . $row[1]);
            $pdf->setDetailCol(20, 5, $row[2]);
            $pdf->setDetailCol(20, 5, $row[3]);
            $pdf->setDetailCol(72, 5, $row[4]);
            $pdf->setDetailCol(20, 5, $row[5]);
            $pdf->setDetailCol(20, 5, $row[6]);
            $pdf->setDetailCol(20, 5, $row[7]);
            $pdf->Ln();
        }
        $stmt = null;
    } catch (PDOException $e) {
        print $e->getMessage();
    }
    $pdf->Output("Reporte de Compras.pdf", "D");
}
function purcharse_supplier_excel() {
    $excel = new Excel();
    $excel->title('Reporte de Compras'); //31 max

    $excel->getActiveSheet()->setTitle();
    $excel->setHeaderCol('B2', 'Número')->setColWidth('B', 20);
    $excel->setHeaderCol('C2', 'Fecha')->setColWidth('C', 20);
    $excel->setHeaderCol('D2', 'RUC')->setColWidth('D', 20);
    $excel->setHeaderCol('E2', 'Proveedor')->setColWidth('E', 50);
    $excel->setHeaderCol('F2', 'SubTotal')->setColWidth('F', 20);
    $excel->setHeaderCol('G2', 'IGV')->setColWidth('G', 20);
    $excel->setHeaderCol('H2', 'Total')->setColWidth('H', 20);

    $count = 3;
    $sql = "SELECT h.serie, h.number, h.date, o.code, o.name, h.total, h.subtotal, h.igv tax, h.operator_id  
                FROM import_document h
                INNER JOIN operators o ON o.id = h.operator_id
                WHERE h.type = 1 AND o.type = 1 AND h.operator_id = :operator_id";
    try {
        $stmt = getConnection()->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
        $stmt->bindParam("operator_id", $_POST["operator_id"]);
        $stmt->execute();
        while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
            $excel->setDetailCol('B' . $count, str_pad($row[0], 3, "0", STR_PAD_LEFT) . " - " . $row[1]);
            $excel->setDetailCol('C' . $count, $row[2]);
            $excel->setDetailCol('D' . $count, $row[3]);
            $excel->setDetailCol('E' . $count, $row[4]);
            $excel->setDetailCol('F' . $count, $row[5]);
            $excel->setDetailCol('G' . $count, $row[6]);
            $excel->setDetailCol('H' . $count, $row[7]);
            $count++;
        }
        $stmt = null;
    } catch (PDOException $e) {
        print $e->getMessage();
    }

    $excel->output();
}

function report_sale_pdf() {
    $pdf = new PDF();
    $pdf->title = "Reporte de Compras";
    $pdf->AddPage();
    //suma de col 192
    $pdf->setHeaderCol(20, 5, 'Número');
    $pdf->setHeaderCol(20, 5, 'Fecha');
    $pdf->setHeaderCol(20, 5, 'RUC');
    $pdf->setHeaderCol(72, 5, 'Proveedor');
    $pdf->setHeaderCol(20, 5, 'SubTotal');
    $pdf->setHeaderCol(20, 5, 'IGV');
    $pdf->setHeaderCol(20, 5, 'Total');
    $pdf->Ln();

    $sql = "SELECT h.serie, h.number, h.date, o.code, o.name, h.total, h.subtotal, h.igv tax, h.operator_id  
                FROM import_document h
                INNER JOIN operators o ON o.id = h.operator_id
                WHERE h.type = 2 AND o.type = 2 AND month(date) = :month";
    try {
        $stmt = getConnection()->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
        $stmt->bindParam("month", $_POST["month"]);
        $stmt->execute();
        while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
            $pdf->setDetailCol(20, 5, str_pad($row[0], 3, "0", STR_PAD_LEFT) . " - " . $row[1]);
            $pdf->setDetailCol(20, 5, $row[2]);
            $pdf->setDetailCol(20, 5, $row[3]);
            $pdf->setDetailCol(72, 5, $row[4]);
            $pdf->setDetailCol(20, 5, $row[5]);
            $pdf->setDetailCol(20, 5, $row[6]);
            $pdf->setDetailCol(20, 5, $row[7]);
            $pdf->Ln();
        }
        $stmt = null;
    } catch (PDOException $e) {
        print $e->getMessage();
    }
    $pdf->Output("Reporte de Compras.pdf", "D");
}
function report_sale_excel() {
    $excel = new Excel();
    $excel->title('Reporte de Compras'); //31 max

    $excel->getActiveSheet()->setTitle();
    $excel->setHeaderCol('B2', 'Número')->setColWidth('B', 20);
    $excel->setHeaderCol('C2', 'Fecha')->setColWidth('C', 20);
    $excel->setHeaderCol('D2', 'RUC')->setColWidth('D', 20);
    $excel->setHeaderCol('E2', 'Proveedor')->setColWidth('E', 50);
    $excel->setHeaderCol('F2', 'SubTotal')->setColWidth('F', 20);
    $excel->setHeaderCol('G2', 'IGV')->setColWidth('G', 20);
    $excel->setHeaderCol('H2', 'Total')->setColWidth('H', 20);

    $count = 3;
    $sql = "SELECT h.serie, h.number, h.date, o.code, o.name, h.total, h.subtotal, h.igv tax, h.operator_id  
                FROM import_document h
                INNER JOIN operators o ON o.id = h.operator_id
                WHERE h.type = 2 AND o.type = 2 AND month(date) = :month";
    try {
        $stmt = getConnection()->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
        $stmt->bindParam("month", $_POST["month"]);
        $stmt->execute();
        while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
            $excel->setDetailCol('B' . $count, str_pad($row[0], 3, "0", STR_PAD_LEFT) . " - " . $row[1]);
            $excel->setDetailCol('C' . $count, $row[2]);
            $excel->setDetailCol('D' . $count, $row[3]);
            $excel->setDetailCol('E' . $count, $row[4]);
            $excel->setDetailCol('F' . $count, $row[5]);
            $excel->setDetailCol('G' . $count, $row[6]);
            $excel->setDetailCol('H' . $count, $row[7]);
            $count++;
        }
        $stmt = null;
    } catch (PDOException $e) {
        print $e->getMessage();
    }

    $excel->output();
}
function report_sale_client_pdf() {
    $pdf = new PDF();
    $pdf->title = "Reporte de Compras";
    $pdf->AddPage();
    //suma de col 192
    $pdf->setHeaderCol(20, 5, 'Número');
    $pdf->setHeaderCol(20, 5, 'Fecha');
    $pdf->setHeaderCol(20, 5, 'RUC');
    $pdf->setHeaderCol(72, 5, 'Proveedor');
    $pdf->setHeaderCol(20, 5, 'SubTotal');
    $pdf->setHeaderCol(20, 5, 'IGV');
    $pdf->setHeaderCol(20, 5, 'Total');
    $pdf->Ln();

    $sql = "SELECT h.serie, h.number, h.date, o.code, o.name, h.total, h.subtotal, h.igv tax, h.operator_id  
                FROM import_document h
                INNER JOIN operators o ON o.id = h.operator_id
                WHERE h.type = 2 AND o.type = 2 AND h.operator_id = :operator_id";
    try {
        $stmt = getConnection()->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
        $stmt->bindParam("operator_id", $_POST["operator_id"]);
        $stmt->execute();
        while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
            $pdf->setDetailCol(20, 5, str_pad($row[0], 3, "0", STR_PAD_LEFT) . " - " . $row[1]);
            $pdf->setDetailCol(20, 5, $row[2]);
            $pdf->setDetailCol(20, 5, $row[3]);
            $pdf->setDetailCol(72, 5, $row[4]);
            $pdf->setDetailCol(20, 5, $row[5]);
            $pdf->setDetailCol(20, 5, $row[6]);
            $pdf->setDetailCol(20, 5, $row[7]);
            $pdf->Ln();
        }
        $stmt = null;
    } catch (PDOException $e) {
        print $e->getMessage();
    }
    $pdf->Output("Reporte de Compras.pdf", "D");
}
function report_sale_client_excel() {
    $excel = new Excel();
    $excel->title('Reporte de Compras'); //31 max

    $excel->getActiveSheet()->setTitle();
    $excel->setHeaderCol('B2', 'Número')->setColWidth('B', 20);
    $excel->setHeaderCol('C2', 'Fecha')->setColWidth('C', 20);
    $excel->setHeaderCol('D2', 'RUC')->setColWidth('D', 20);
    $excel->setHeaderCol('E2', 'Proveedor')->setColWidth('E', 50);
    $excel->setHeaderCol('F2', 'SubTotal')->setColWidth('F', 20);
    $excel->setHeaderCol('G2', 'IGV')->setColWidth('G', 20);
    $excel->setHeaderCol('H2', 'Total')->setColWidth('H', 20);

    $count = 3;
    $sql = "SELECT h.serie, h.number, h.date, o.code, o.name, h.total, h.subtotal, h.igv tax, h.operator_id  
                FROM import_document h
                INNER JOIN operators o ON o.id = h.operator_id
                WHERE h.type = 2 AND o.type = 2 AND h.operator_id = :operator_id";
    try {
        $stmt = getConnection()->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
        $stmt->bindParam("operator_id", $_POST["operator_id"]);
        $stmt->execute();
        while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
            $excel->setDetailCol('B' . $count, str_pad($row[0], 3, "0", STR_PAD_LEFT) . " - " . $row[1]);
            $excel->setDetailCol('C' . $count, $row[2]);
            $excel->setDetailCol('D' . $count, $row[3]);
            $excel->setDetailCol('E' . $count, $row[4]);
            $excel->setDetailCol('F' . $count, $row[5]);
            $excel->setDetailCol('G' . $count, $row[6]);
            $excel->setDetailCol('H' . $count, $row[7]);
            $count++;
        }
        $stmt = null;
    } catch (PDOException $e) {
        print $e->getMessage();
    }

    $excel->output();
}


?>